package com.xbongbong.pro.rabbitmq.consumer;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.message.pojo.vo.MessageRabbitMqVO;
import com.xbongbong.pro.product.pojo.dto.CostRecalculationDTO;
import com.xbongbong.pro.rabbitmq.binders.CostRecalculationConsumerBinder;
import com.xbongbong.pro.rabbitmq.constant.CostRecalculationConstant;
import com.xbongbong.pro.rabbitmq.feign.CostRecalculstionFeignClient;
import com.xbongbong.pro.rabbitmq.feign.JxcFeignClient;
import feign.RetryableException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 成本重算消费者
 * @author jiping.yang
 * @date 2021/12/2 10:13
 */
@EnableBinding(CostRecalculationConsumerBinder.class)
public class CostRecalculationListener {

    private static Logger logger = LoggerFactory.getLogger(CostRecalculationListener.class);

    @Resource
    private CostRecalculstionFeignClient costRecalculstionFeignClient;

    @StreamListener(value = CostRecalculationConstant.COST_RECALCULATION_INPUT)
    public void costRecalculation(CostRecalculationDTO message){
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            if (Objects.equals(message.getMark() , 2)){
                response = costRecalculstionFeignClient.costRecalculationDelete(message);
            }else {
                response = costRecalculstionFeignClient.costRecalculationAdd(message);
            }
            logger.info("message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("JxcListener.supplierReceive RetryableException case by Read timed out");
        } catch (Exception e) {
            logger.error("JxcListener.supplierReceive Exception, type=", e);
        }
    }
}
